Vue.component('yl-tencent-map', {
    template: `<div id="map-container" :style="{width,height}"></div>`,
    props: {
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '200px'
        },
        center: {
            type: Array || Object,
            default: function () {
                return []
            }
        },
        zoom: {
            type: Number,
            default: 15
        }
    },
    data() {
        return {
            default:[39.916527, 116.397128]
        }
    },
    computed: {
        MapCenter() {
            if (Array.isArray(this.center)) {
                const latitudeCheck = Number.isFinite(this.center[0]) && this.center[0] >= -90 && this.center[0] <= 90;
                const longitudeCheck = Number.isFinite(this.center[1]) && this.center[1] >= -180 && this.center[1] <= 180;
                return latitudeCheck && longitudeCheck ? this.center : this.default
            } else {
                const latitudeCheck = Number.isFinite(this.center['latitude']) && this.center['latitude'] >= -90 && this.center[0] <= 90;
                const longitudeCheck = Number.isFinite(this.center['longitude']) && this.center['longitude'] >= -180 && this.center[1] <= 180;
                return latitudeCheck && longitudeCheck ? [this.center['latitude'], this.center['longitude']] : this.default
            }
        }
    },
    methods: {
        init() {
            if (window.TMap) {
                this.map = new window.TMap.Map('map-container', {
                    zoom: this.zoom, // 初始缩放级别
                    center: new window.TMap.LatLng(...this.MapCenter),
                });
            } else {
                console.error('腾讯地图API未加载完成');
            }
        }
    },
    mounted() {
        this.init();
    },
})
new Vue({
    el: '#app',
    data() {
        return {
            rules: {},
            loading: false,
            form: {
                Company_Business_Personnel_SerialNumber: "",
                Company_Business_Personnel_Name: '',
                Company_Business_Personnel_Position: "",
                Company_Business_Personnel_Phone: "",
            },
        }
    },
    mounted() {

    },
    methods: {
        initMap() {
            if (window.TMap) {

                this.map = new window.TMap.Map('tencent-map-container', {
                    zoom: 15, // 初始缩放级别
                    center: new window.TMap.LatLng(39.916527, 116.397128), // 初始中心经纬度（例如北京的位置）
                });
            } else {
                console.error('腾讯地图API未加载完成');
            }
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    $.ajax({
                        url: DomainName + '/getAddAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({
                                company_business_personnel: [_this.form]
                            }),
                            Operation_Name: '添加业务人员'
                        },
                        success: function (res) {
                            if (res.Status == 'success') {
                                _this.$message.success("发布成功!")
                                window.history.go(-1)
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    return false;
                }
            });
        },
    },
})